<?php
/**
 * file: auth.class.php
 * 
 * 权限控制操作类
 * 
 * @author tommy
 * @package extension
 * @version 1.0
 */

class auth extends Base {
	
	/**
	 * 权限验证
	 * 
	 * 判断用户是否拥有某种权限
	 * @param integer $user_id 			用户ID
	 * @param string  $acl_option_name 	权限名
	 * @return boolean
	 */
	public function check_auth($user_id, $acl_option_name) {
		
		//parse params
		if (empty($user_id) || empty($acl_option_name)) {
			return false;
		}
		
		//instance model
		$acl_option_model = Controller::model('acl_options');
		
		//get user acl option data
		$acl_option_id = $acl_option_model->getOne('auth_option_name=?', $acl_option_name);
		if (!$acl_option_id) {
			return false;
		}
		
		$acl_option_id 		= $acl_option_id['auth_option_id'];		
		$acl_option_data 	= $acl_option_model->from('acl_users', 'auth_option_id')->where('user_id=' . $user_id)->where('auth_option_id=' . $acl_option_id)->where('auth_setting=1')->select(false);
		
		return (!empty($acl_option_data)) ? true : false;
	}
	
	/**
	 * 获取用户的权限
	 * 
	 * @param integer $user_id 用户ID
	 * @return array
	 */
	public function get_user_auth($user_id) {
		
		//parse params
		if (!$user_id) {
			return false;
		}
		
		//instance model		
		$acl_option_model = Controller::model('acl_options');
		
		$user_auth_data = $acl_option_model->from(array('au'=>'acl_users', 'ao'=>'acl_options'), 'ao.auth_option_name')->where('au.user_id=' . $user_id)->where('au.auth_setting=1')->where('au.auth_option_id=ao.	auth_option_id')->select();
		
		$auth_array = array();
		foreach($user_auth_data as $lines) {
			$auth_array[] = trim($lines['auth_option_name']);
		}
		
		return $auth_array;
	}
}